﻿namespace MSSqlSchemaDoc.Core.DataStructures
{
    using System;
    using System.Collections.ObjectModel;
    using System.Xml;
    using System.Xml.Serialization;

    /// <summary>
    /// Represents a user defined function.
    /// </summary>
    [Serializable()]
    public class UserDefinedFunction : DataStructureObjectBase
    {
        /// <summary>
        /// Initializes a new instance of the UserDefinedFunction class.
        /// </summary>
        public UserDefinedFunction()
        {
            this.Parameters = new Collection<ProcedureParameter>();
        }

        /// <summary>
        /// Gets the UDF object type string.
        /// </summary>
        public override DataStructureObjectType ObjectType
        {
            get
            {
                return DataStructureObjectType.UserDefinedFunction;
            }
        }

        /// <summary>
        /// Gets or sets the parameter list.
        /// </summary>
        [XmlArray()]
        public Collection<ProcedureParameter> Parameters { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether ANSI_NULLS are used in the procedure.
        /// </summary>
        [XmlElement()]
        public bool UsesAnsiNulls { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether QUOTED_IDENTIFIERS are used in the procedure.
        /// </summary>
        [XmlElement()]
        public bool UsesQuotedIdentifier { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether the procedure is schema bound.
        /// </summary>
        [XmlElement()]
        public bool IsSchemaBound { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether the procedure uses database collation.
        /// </summary>
        [XmlElement()]
        public bool UsesDatabaseCollation { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether the procedure is recompiled.
        /// </summary>
        [XmlElement()]
        public bool IsRecompiled { get; set; }

        /// <summary>
        /// Gets or sets a value indicating which principal ID the procedure executes as.
        /// </summary>
        [XmlElement()]
        public int? ExecuteAsPrincipalId { get; set; }

        /// <summary>
        /// Gets or sets the text body of the stored procedure.
        /// </summary>
        [XmlIgnore()]
        public string Definition { get; set; }

        /// <summary>
        /// Gets or sets the definition wrapped as a "CData" section.
        /// Only used for serialization.
        /// </summary>
        [XmlElement("Definition")]
        public XmlCDataSection XmlCDataDefinition
        {
            get
            {
                XmlDocument d = new XmlDocument();
                return d.CreateCDataSection(this.Definition);
            }

            set
            {
                this.Definition = value.CreateNavigator().InnerXml;
            }
        }
    }
}
